<?php
namespace app\controller;

use app\domain\car;
use rap\config\Config;
use rap\exception\MsgException;
use rap\web\Request;
use rap\web\Response;
use app\domain\user;

/**
 * User: jinghao@duohuo.net
 * Date: 2018/12/30
 * Time: 2:55 PM
 * Link:  http://magapp.cc
 * Copyright:南京灵衍信息科技有限公司
 */
class IndexController {


    /**
     * 登录
     * @param Request $request
     * @param string  $account
     * @param string  $password
     * @return array
     */
    public function index(Request $request, Response $response) {
        if ($request->method() == 'GET') {

             $agent = user::getUserToken($_SERVER['HTTP_USER_AGENT']);

             $token = $agent['token'];
             if (!$token) {
                 return CommonController::false(402,"请先登录");
             }
             //查询用户信息是否存在
             $res = CommonController::getUserInfo($token);
             $data = $res['data'];
             unset($data['regist_time']);
             $data['token'] = $token;
             $data['nickname'] = $data['name'];
             unset($data['name']);
             $data['update_time'] = date("Y-m-d H:i:s", time());
             $response->assign("token", $token);
             //判断是否存在
             if (!$data){
                 return CommonController::false();
             } else {
                 $where = [
                     'user_id' => $data['user_id'],
                     'is_del' => 0
                 ];
                 $res = user::getUserAuthInfo($where, "*");
                 $gold = user::getUserGoldCount($data['user_id']);
                 if ($res) {
                     user::updateUserAuth($where, $data);
                 } else {
                     user::insertUserAuth($data);

                 }
             }
             session_start();
             $data['gold'] = $gold;

             $_SESSION[$token] = $data;
            return 'index';
        }
    }

}